import Cookies from 'js-cookie'
import { App } from 'vue-demi'

const tkn = 'ua'

class Token {
  static get() {
    return Cookies.get(tkn)
  }

  static set(value: string) {
    Cookies.set(tkn, value, { expires: 0.0417 })
  }

  static has() {
    return !!this.get()
  }

  static delete() {
    Cookies.remove(tkn)
  }

  static refresh() {
    const token = this.get()
    if (token) {
      this.set(token)
    }
  }

  static id(value?: string | number): string {
    if (value) {
      localStorage.setItem('uaid', `${value}`)
    }
    return localStorage.getItem('uaid') ?? ''
  }
}

export { Token }

export default function token(app: App) {
  app.config.globalProperties.$token = Token
}
